package com.knight.redis.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import com.knight.redis.entity.Person;

/**
 * 
 * @author fox
 *
 */
@Repository
public class RedisTestDao {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Resource(name = "stringRedisTemplate")
	private ValueOperations<String, String> valueOperations;

	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	@Resource(name = "redisTemplate")
	private ValueOperations<Object, Object> valOps;

	public void stringRedisTemplate() {
		valueOperations.set("xx", "yy");
	}

	public void save(Person person) {
		valOps.set(person.getId(), person);
	}

	public String getString() {
		return valueOperations.get("xx");
	}

	public Person getPerson() {
		return (Person) valOps.get("1");
	}

	public void setList() {
		List<Person> persons = new ArrayList<>();
		for (int i = 0; i < 100; i++) {
			persons.add(new Person(String.valueOf(i), "刘武" + i, 21));
		}
		valOps.set("persons", persons);
	}

	@SuppressWarnings("unchecked")
	public List<Person> getList() {
		List<Person> persons = (List<Person>)valOps.get("persons");
		return persons;
	}
	
	public void setMap() {
		HashMap<String, List<Person>> hashMap = new HashMap<String,List<Person>>();
		
		for(int j=0;j<100;j++) {
			List<Person> persons = new ArrayList<>();
			for (int i = 0; i < 100; i++) {
				persons.add(new Person(String.valueOf(i), "刘武" + i, 21));
			}
			hashMap.put(String.valueOf(j), persons);
		}
		
		valOps.set("maps", hashMap);
		
	}
	
	@SuppressWarnings("unchecked")
	public HashMap<String, List<Person>> getMap() {
		HashMap<String, List<Person>> hashMap = (HashMap<String, List<Person>>) valOps.get("maps");
		return hashMap;
		
	}
}
